package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.mapper.SystemDictionaryMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    @Transactional
    public void save(SystemDictionary systemDictionary) {
        systemDictionaryMapper.insert(systemDictionary);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        systemDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(SystemDictionary systemDictionary) {
        systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
    }

    @Override
    public SystemDictionary get(Long id) {
        return systemDictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> listAll() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionary> query(QueryObject qo) {
        // 告诉插件我们查询的是第几页，每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        // 查询当前页的数据
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectForList(qo);
        return new PageInfo<>(systemDictionarys);
    }

    @Override
    public List<Map> queryTreeData() {
        //查询顶级数据目录(父节点)-parent_id 为null
        List<SystemDictionary> all = systemDictionaryMapper.selectAll();
        List<SystemDictionary> top = new ArrayList<>();
        for (SystemDictionary item : all) {
            if (item.getParentId() == null) {
                top.add(item);
                item.setChildren(findChildren(all, item.getId()));
            }
        }

        fillChildren(all, top);


        //页面的treeview插件用到属性: text 跟nodes
        String str = JSON.toJSONString(top);
        str = str.replaceAll("name", "text").replaceAll("children", "nodes");

        List<Map> maps = JSON.parseArray(str, Map.class);


        return maps;
    }

    //填充儿子
    private void fillChildren(List<SystemDictionary> all, List<SystemDictionary> children) {
        if (children == null || children.size() == 0) {
            return;
        }
        for (SystemDictionary item : children) {

            item.setChildren(findChildren(all, item.getId()));

            fillChildren(all, item.getChildren());
        }
    }

    // 参数1:所有数据, 参数2:父id
    private List<SystemDictionary> findChildren(List<SystemDictionary> all, Long parentId) {
        List<SystemDictionary> children = new ArrayList<>();
        for (SystemDictionary item : all) {
            if (item.getParentId() == parentId) {
                children.add(item);
            }
        }
        return children;
    }


    @Override
    public List<SystemDictionary> queryBySn(String sn) {
        return systemDictionaryMapper.selectBySn(sn);
    }

    @Override
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryMapper.selectByParentId(parentId);
    }
}
